﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.Controls
{
    public partial class StockLookupCtrl : BaseControl
    {
        public delegate void ItemDoubleClick(Object sender, ListItemEventArgs e);
        ItemDoubleClick onItemDoubleClick;

        public ItemDoubleClick OnItemDoubleClick
        {
            get { return onItemDoubleClick; }
            set { onItemDoubleClick = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                StockLocation stocks = StockLocation.GetAllActiceStockLocation();
                Utilities.LoadDropDownListNoSelect(ddlLocation, stocks.DefaultView, StockLocation.ColumnNames.LocationName, StockLocation.ColumnNames.ID);

                Brands brands = Brands.LoadAllActiveBrands();
                Utilities.LoadDropDownList(ddlBrand, brands.DefaultView, Brands.ColumnNames.BrandName, Brands.ColumnNames.ID, "--All--");

                RefreshProductList();
                RefreshStockList();
            }
        }

        public void OnSelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshProductList();
        }

        public void RefreshProductList()
        {
            int brandID = String.IsNullOrEmpty(ddlBrand.SelectedValue) ? 0 : int.Parse(ddlBrand.SelectedValue);

            Products prod = Products.GetIMEIActiveProductByBrandID(brandID);

            Utilities.LoadDropDownList(ddlProduct, prod.DefaultView, Products.ColumnNames.ProductName, Products.ColumnNames.ID);
        }

        public void ddlProduct_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlProduct.SelectedIndex > 0)
            {
                RefreshStockList();
            }
        }

        public void RefreshStockList()
        {
            int stockID = String.IsNullOrEmpty(ddlLocation.SelectedValue) ? 0 : int.Parse(ddlLocation.SelectedValue);
            int productID = String.IsNullOrEmpty(ddlProduct.SelectedValue) ? 0 : int.Parse(ddlProduct.SelectedValue);
            View_ProductDetails prod = View_ProductDetails.GetAvailableProductDetailsByProductIDLocationID(productID, stockID);
            lstProduct.SourceObject = prod;
            lstProduct.Refresh();
            lstProduct.Columns[0].Visible = false;
            gridFilterPaging.Reset();
        }

        public void gridFilterPaging_OnNeedListControl(object sender, EventArgs e)
        {
            gridFilterPaging.ListControl = lstProduct;
        }

        public String GridClientID
        {
            get
            {
                return lstProduct.ClientID;
            }
        }

        public String OnDblClick
        {
            get
            {
                return lstProduct.OnDblClick;
            }
            set { lstProduct.OnDblClick = value; }
        }
    }
}